
import numpy as np
from scipy.spatial.distance import cdist

# 生成高维向量数据
def generate_high_dim_vectors(num_points=500, dimensions=50):
    """生成随机高维向量"""
    np.random.seed(42) # 确保结果可重复
    data = np.random.rand(num_points, dimensions)
    return data

# 生成 50 维随机向量数据
high_dim_vectors = generate_high_dim_vectors(num_points=500, dimensions=50)

# 计算每个维度的方差
variances = np.var(high_dim_vectors, axis=0)
print("每个维度的方差:")
print(variances)

# 计算向量间的欧氏距离分布
pairwise_distances = cdist(high_dim_vectors, high_dim_vectors, metric='euclidean')
mean_distance = np.mean(pairwise_distances)
std_distance = np.std(pairwise_distances)

print("\n向量间欧氏距离的平均值:", mean_distance)
print("向量间欧氏距离的标准差:", std_distance)

# 计算每个向量的最近邻距离 - 修正版本
# 方法1: 将对角线设置为一个很大的值而不是无穷大
masked_distances = pairwise_distances + np.eye(pairwise_distances.shape[0]) * (np.max(pairwise_distances) + 1)
nearest_distances = np.min(masked_distances, axis=1)

# 方法2: 或者使用更直接的方式 - 对每行排序并取第二个最小的值（因为最小值是0，即自身）
# nearest_distances = np.partition(pairwise_distances, 1, axis=1)[:, 1]

mean_nearest_distance = np.mean(nearest_distances)

print("\n最近邻距离的平均值:", mean_nearest_distance)

# 检查距离分布是否均匀
distance_variation_ratio = std_distance / mean_distance
print("\n距离分布的变异系数（标准差/平均值）:", distance_variation_ratio)